<template>
  <div class="chat-app">
    <!-- 顶部标题栏 -->
    <div class="app-header">
      <div class="header-content">
        <i class="el-icon-chat-line-round"></i>
        <h1>即时通讯系统</h1>
      </div>
    </div>

    <!-- 主内容区 -->
    <div class="app-main">
      <!-- 连接控制区 -->
      <el-card class="control-panel">
        <div class="panel-title">
          <i class="el-icon-connection"></i>
          <span>连接设置</span>
          <el-tag :type="isConnected ? 'success' : 'info'" size="small">
            {{ isConnected ? '已连接' : '未连接' }}
          </el-tag>
        </div>
        <el-form inline class="control-form">
          <el-input
              v-model="uid"
              placeholder="输入用户ID"
              clearable
              prefix-icon="el-icon-user"
              class="user-input"
          ></el-input>
          <el-button
              type="primary"
              @click="conn"
              :icon="isConnected ? 'el-icon-success' : 'el-icon-link'"
              class="connect-btn"
          >
            {{ isConnected ? '已连接' : '连接服务器' }}
          </el-button>
        </el-form>
      </el-card>

      <!-- 消息发送区 -->
      <el-card class="message-panel">
        <div class="panel-title">
          <i class="el-icon-edit-outline"></i>
          <span>发送消息</span>
        </div>
        <el-form class="message-form">
          <el-form-item label="接收方ID">
            <el-input
                v-model="toUser"
                placeholder="输入接收方ID"
                clearable
                prefix-icon="el-icon-user-solid"
            ></el-input>
          </el-form-item>
          <el-form-item label="消息内容">
            <el-input
                v-model="msg"
                type="textarea"
                :rows="3"
                placeholder="输入消息内容..."
                resize="none"
                show-word-limit
                maxlength="200"
            ></el-input>
          </el-form-item>
          <el-button
              type="success"
              @click="send"
              :disabled="!canSend"
              class="send-btn"
          >
            <i class="el-icon-position"></i> 发送消息
          </el-button>
        </el-form>
      </el-card>

      <!-- 消息展示区 -->
      <el-card class="history-panel">
        <div class="panel-title">
          <i class="el-icon-chat-dot-round"></i>
          <span>消息记录</span>
          <el-button
              type="text"
              @click="clearMessages"
              :disabled="msgList.length === 0"
              class="clear-btn"
          >
            <i class="el-icon-delete"></i> 清空记录
          </el-button>
        </div>
        <div class="message-container" ref="messageContainer">
          <div v-if="msgList.length === 0" class="empty-message">
            <i class="el-icon-chat-line-square"></i>
            <p>暂无消息记录</p>
          </div>
          <div
              v-for="(m, index) in msgList"
              :key="index"
              class="message-item"
          >
            <div class="message-content">{{ m }}</div>
            <div class="message-time">{{ formatTime(Date.now()) }}</div>
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, nextTick } from "vue";
import axios from "axios";
import { ElMessage } from "element-plus";

let uid = ref('');
let toUser = ref('');
let msg = ref('');
let msgList = ref([]);
let isConnected = ref(false);

const canSend = computed(() => {
  return isConnected.value && toUser.value && msg.value;
});

const formatTime = (timestamp) => {
  return new Date(timestamp).toLocaleTimeString();
};

let conn = () => {
  let ws = new WebSocket('ws://localhost:8081/socket/' + uid.value);
  ws.onmessage = (m) => {
    msgList.value.push(m.data);
    scrollToBottom();
  }
  ws.onopen = () => {
    isConnected.value = true;
    ElMessage.success('连接成功');
  }
  ws.onclose = () => {
    isConnected.value = false;
    ElMessage.warning('连接已关闭');
  }
}

let send = () => {
  axios.post(`/send?uid=${toUser.value}&message=${msg.value}`).then(res => {
    ElMessage.success(res.msg);
    msg.value = '';
  })
}

const clearMessages = () => {
  msgList.value = [];
  ElMessage.info('已清空消息记录');
};

const messageContainer = ref(null);
const scrollToBottom = () => {
  nextTick(() => {
    if (messageContainer.value) {
      messageContainer.value.scrollTop = messageContainer.value.scrollHeight;
    }
  });
};
</script>

<style scoped>
.chat-app {
  max-width: 900px;
  margin: 0 auto;
  padding: 20px;
  font-family: 'Helvetica Neue', Helvetica, 'PingFang SC',
  'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;
  background-color: #f8fafc;
  min-height: 100vh;
}

.app-header {
  background: linear-gradient(135deg, #3a7bd5 0%, #00d2ff 100%);
  color: white;
  padding: 20px;
  border-radius: 12px;
  margin-bottom: 24px;
  text-align: center;
  box-shadow: 0 4px 20px rgba(58, 123, 213, 0.2);
}

.header-content {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.header-content i {
  font-size: 48px;
  margin-bottom: 10px;
}

.header-content h1 {
  margin: 0;
  font-size: 28px;
  font-weight: 500;
}

.app-main {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.el-card {
  border-radius: 12px;
  border: none;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
}

.el-card:hover {
  box-shadow: 0 6px 16px 0 rgba(0, 0, 0, 0.12);
}

.panel-title {
  display: flex;
  align-items: center;
  margin-bottom: 16px;
  font-size: 16px;
  font-weight: 500;
  color: #3a7bd5;
}

.panel-title i {
  margin-right: 8px;
  font-size: 18px;
}

.control-form {
  display: flex;
  gap: 12px;
}

.user-input {
  flex: 1;
}

.connect-btn {
  min-width: 120px;
}

.message-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.send-btn {
  width: 100%;
  height: 42px;
  font-size: 16px;
}

.message-container {
  max-height: 400px;
  overflow-y: auto;
  padding: 10px;
  background-color: #f9fafb;
  border-radius: 8px;
}

.empty-message {
  text-align: center;
  padding: 60px 0;
  color: #909399;
}

.empty-message i {
  font-size: 60px;
  margin-bottom: 16px;
  color: #e0e6ed;
}

.empty-message p {
  font-size: 16px;
}

.message-item {
  margin-bottom: 16px;
  animation: fadeIn 0.3s ease;
}

.message-content {
  padding: 12px 16px;
  background-color: white;
  border-radius: 12px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
  word-break: break-word;
  line-height: 1.5;
}

.message-time {
  font-size: 12px;
  color: #909399;
  margin-top: 4px;
  text-align: right;
}

.clear-btn {
  margin-left: auto;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 768px) {
  .chat-app {
    padding: 12px;
  }

  .control-form {
    flex-direction: column;
  }

  .connect-btn {
    width: 100%;
  }

  .app-header {
    padding: 16px;
  }

  .header-content h1 {
    font-size: 24px;
  }
}
</style>